//
//  LGTabbar.swift
//  LeGou
//
//  Created by lxm on 2022/7/27.
//

import UIKit
import RxSwift

class LGTabbar: UITabBar {
 
    //MARK: --------------------属性--------------------
    let disposeBag = DisposeBag()
    
    var centerClick : (()->())?
    
    let contentH : CGFloat = 80
    //MARK: --------------------UI控件--------------------
    let mallButton = UIButton().then { b in
        b.setImage(UIImage.init(named: "vip_mall"), for: .normal)
        b.setImage(UIImage.init(named: "vip_mall_h"), for: .selected)
        b.sizeToFit()
    }
    
    lazy var btn = makeBtnView()
    
    let player = SVGAPlayer.init(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
    
    //MARK: --------------------生命周期--------------------
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
        bindAction()
        addConstraints()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        setUI()
        bindAction()
        addConstraints()
    }

    //MARK: --------------------UI--------------------
    func setUI() {
//        addSubview(mallButton)
        if !AppHelper.appVerify() {
            addSubview(btn)
            player.loops = Int32.max
            player.clearsAfterStop = false
            let parser = SVGAParser()
            parser.parse(withNamed: "paimaihuisvga", in: nil) { videoItem in
                self.player.videoItem = videoItem
                self.player.startAnimation()
            }
        }
    }
    
    func addConstraints() {
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        if AppHelper.appVerify() {
            return
        }
        let count = items?.count ?? 2
        let width = SCREEN_W / CGFloat(count)
        var index = 0
        for fv in self.subviews {
            if fv.classForCoder == NSClassFromString("UITabBarButton") {
                if index == 2 && fv.zm.y > 0 {
//                    fv.zm.setY(fv.zm.y - 10)
                    guard let lab = fv.value(forKey: "_label") as? UILabel else {
                        continue
                    }
                    if lab.text == MainVC.CENTER_NAME {
                        fv.alpha = 0
                    }
                }
//                print(fv.frame)
                index += 1
            }
            print()
        }
        btn.zm.setCenterX(zm.centerX)
        btn.zm.setY(-30)
        print(btn.frame)
    }
    
    //MARK: --------------------methods--------------------
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        if isHidden == false, !AppHelper.appVerify() {
            let p = layer.convert(point, to: self.btn.layer)
            if self.btn.layer.contains(p) {
                return self.btn
            }
        }
        return super.hitTest(point, with: event)
    }
    
    func bindAction() {
       
    }
    
    func makeBtnView() -> UIView {
        let v = UIView()
        v.frame = CGRect(x: 0, y: 0, width: 64, height: contentH)
//        v.backgroundColor = .red
        v.zm.addTap(target: self, selector: #selector(onCenter))
        
        let bg = UIView(frame: CGRect(x: 0, y: 0, width: 64, height: 64))
        bg.style.GradientLeftToRight(Top: "#FFD3D3".zm.hex(), Bottom: "#FFFFFF".zm.hex())
        bg.layer.cornerRadius = 32
        bg.layer.masksToBounds =  true
        bg.borderWidth = 2
        bg.borderColor = .white
        v.addSubview(bg)
        
        v.addSubview(player)
        player.center = bg.center
        
        let btn = ZMUtil.button(title: "趣味竞购", font: UIFont.systemFont(ofSize: 12, weight: .medium), color: .white)
        btn.frame = CGRect(x: 0, y: contentH - 22, width: 64, height: 22)
        btn.style.GradientLeftToRight(left: "#F07650".zm.hex(), right: "#DC1927".zm.hex())
        btn.layer.cornerRadius = 11
        btn.layer.masksToBounds = true
        v.addSubview(btn)
        return v
    }
    
    @objc func onCenter() {
        if let centerClick = centerClick {
            centerClick()
        }
    }
    //MARK: --------------------delegate--------------------

}
